﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Practices.Prism.Commands;
using ParaEditor.Common;
using ParaEditor.DAO.ParaDAO;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Desktop.View;
using ParaEditor.Domain.Entities;
using ParaEditor.Infrastructure;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(IPara3083ViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class Para3083ViewModel : ViewModelBase, IPara3083ViewModel
    {
        private Para3083DAO paramDao;


        private ICollectionView infoVerList;
        public ICollectionView InfoVerList
        {
            get
            {
                return infoVerList;
            }
            set
            {
                this.infoVerList = value;
                this.RaisePropertyChangedEvent("InfoVerList");
            }
        }

        private ICollectionView fromIndexList;
        public ICollectionView FromIndexList
        {
            get
            {
                return fromIndexList;
            }
            set
            {
                this.fromIndexList = value;
                this.RaisePropertyChangedEvent("FromIndexList");
            }
        }

        private Visibility editGroupBoxVisible;
        public Visibility EditGroupBoxVisible
        {
            get
            {
                return this.editGroupBoxVisible;
            }
            set
            {
                this.editGroupBoxVisible = value;
                this.RaisePropertyChangedEvent("EditGroupBoxVisible");
            }
        }

        private string iMAGENAME;
        public string IMAGENAME
        {
            get
            {
                return this.iMAGENAME;
            }
            set
            {
                this.iMAGENAME = value;
                this.RaisePropertyChangedEvent("IMAGENAME");
            }
        }

        private int iMAGEHEIGHT;
        public int IMAGEHEIGHT
        {
            get
            {
                return this.iMAGEHEIGHT;
            }
            set
            {
                this.iMAGEHEIGHT = value;
                this.RaisePropertyChangedEvent("IMAGEHEIGHT");
            }
        }

        private int iMAGEWIDTH;
        public int IMAGEWIDTH
        {
            get
            {
                return this.iMAGEWIDTH;
            }
            set
            {
                this.iMAGEWIDTH = value;
                this.RaisePropertyChangedEvent("IMAGEWIDTH");
            }
        }

        private int bUTTONX;
        public int BUTTONX
        {
            get
            {
                return this.bUTTONX;
            }
            set
            {
                this.bUTTONX = value;
                this.RaisePropertyChangedEvent("BUTTONX");
            }
        }

        private int bUTTONY;
        public int BUTTONY
        {
            get
            {
                return this.bUTTONY;
            }
            set
            {
                this.bUTTONY = value;
                this.RaisePropertyChangedEvent("BUTTONY");
            }
        }

        private short lANGLENGTH;
        public short LANGLENGTH
        {
            get
            {
                return this.lANGLENGTH;
            }
            set
            {
                this.lANGLENGTH = value;
                this.RaisePropertyChangedEvent("LANGLENGTH");
            }
        }

        private short lANGWIDTH;
        public short LANGWIDTH
        {
            get
            {
                return this.lANGWIDTH;
            }
            set
            {
                this.lANGWIDTH = value;
                this.RaisePropertyChangedEvent("LANGWIDTH");
            }
        }

        private int bUTTONX_1;
        public int BUTTONX_1
        {
            get
            {
                return this.bUTTONX_1;
            }
            set
            {
                this.bUTTONX_1 = value;
                this.RaisePropertyChangedEvent("BUTTONX_1");
            }
        }

        private int bUTTONY_1;
        public int BUTTONY_1
        {
            get
            {
                return this.bUTTONY_1;
            }
            set
            {
                this.bUTTONY_1 = value;
                this.RaisePropertyChangedEvent("BUTTONY_1");
            }
        }

        private short bUTTONLENGTH_1;
        public short BUTTONLENGTH_1
        {
            get
            {
                return this.bUTTONLENGTH_1;
            }
            set
            {
                this.bUTTONLENGTH_1 = value;
                this.RaisePropertyChangedEvent("BUTTONLENGTH_1");
            }
        }

        private short bUTTONWIDTH_1;
        public short BUTTONWIDTH_1
        {
            get
            {
                return this.bUTTONWIDTH_1;
            }
            set
            {
                this.bUTTONWIDTH_1 = value;
                this.RaisePropertyChangedEvent("BUTTONWIDTH_1");
            }
        }

        private short bUTTONVALID_1;
        public short BUTTONVALID_1
        {
            get
            {
                return this.bUTTONVALID_1;
            }
            set
            {
                this.bUTTONVALID_1 = value;
                this.RaisePropertyChangedEvent("BUTTONVALID_1");
            }
        }

        private short bUTTONFORMINDEX_1;
        public short BUTTONFORMINDEX_1
        {
            get
            {
                return this.bUTTONFORMINDEX_1;
            }
            set
            {
                this.bUTTONFORMINDEX_1 = value;
                this.RaisePropertyChangedEvent("BUTTONFORMINDEX_1");
            }
        }

        private int bUTTONX_2;
        public int BUTTONX_2
        {
            get
            {
                return this.bUTTONX_2;
            }
            set
            {
                this.bUTTONX_2 = value;
                this.RaisePropertyChangedEvent("BUTTONX_2");
            }
        }

        private short bUTTONY_2;
        public short BUTTONY_2
        {
            get
            {
                return this.bUTTONY_2;
            }
            set
            {
                this.bUTTONY_2 = value;
                this.RaisePropertyChangedEvent("BUTTONY_2");
            }
        }

        private short bUTTONLENGTH_2;
        public short BUTTONLENGTH_2
        {
            get
            {
                return this.bUTTONLENGTH_2;
            }
            set
            {
                this.bUTTONLENGTH_2 = value;
                this.RaisePropertyChangedEvent("BUTTONLENGTH_2");
            }
        }

        private short bUTTONWIDTH_2;
        public short BUTTONWIDTH_2
        {
            get
            {
                return this.bUTTONWIDTH_2;
            }
            set
            {
                this.bUTTONWIDTH_2 = value;
                this.RaisePropertyChangedEvent("BUTTONWIDTH_2");
            }
        }

        private int bUTTONVALID_2;
        public int BUTTONVALID_2
        {
            get
            {
                return this.bUTTONVALID_2;
            }
            set
            {
                this.bUTTONVALID_2 = value;
                this.RaisePropertyChangedEvent("BUTTONVALID_2");
            }
        }

        private int bUTTONFORMINDEX_2;
        public int BUTTONFORMINDEX_2
        {
            get
            {
                return this.bUTTONFORMINDEX_2;
            }
            set
            {
                this.bUTTONFORMINDEX_2 = value;
                this.RaisePropertyChangedEvent("BUTTONFORMINDEX_2");
            }
        }

        private int bUTTONX_3;
        public int BUTTONX_3
        {
            get
            {
                return this.bUTTONX_3;
            }
            set
            {
                this.bUTTONX_3 = value;
                this.RaisePropertyChangedEvent("BUTTONX_3");
            }
        }

        private short bUTTONY_3;
        public short BUTTONY_3
        {
            get
            {
                return this.bUTTONY_3;
            }
            set
            {
                this.bUTTONY_3 = value;
                this.RaisePropertyChangedEvent("BUTTONY_3");
            }
        }

        private short bUTTONLENGTH_3;
        public short BUTTONLENGTH_3
        {
            get
            {
                return this.bUTTONLENGTH_3;
            }
            set
            {
                this.bUTTONLENGTH_3 = value;
                this.RaisePropertyChangedEvent("BUTTONLENGTH_3");
            }
        }

        private short bUTTONWIDTH_3;
        public short BUTTONWIDTH_3
        {
            get
            {
                return this.bUTTONWIDTH_3;
            }
            set
            {
                this.bUTTONWIDTH_3 = value;
                this.RaisePropertyChangedEvent("BUTTONWIDTH_3");
            }
        }

        private int bUTTONVALID_3;
        public int BUTTONVALID_3
        {
            get
            {
                return this.bUTTONVALID_3;
            }
            set
            {
                this.bUTTONVALID_3 = value;
                this.RaisePropertyChangedEvent("BUTTONVALID_3");
            }
        }

        private int bUTTONFORMINDEX_3;
        public int BUTTONFORMINDEX_3
        {
            get
            {
                return this.bUTTONFORMINDEX_3;
            }
            set
            {
                this.bUTTONFORMINDEX_3 = value;
                this.RaisePropertyChangedEvent("BUTTONFORMINDEX_3");
            }
        }

        private int bUTTONX_4;
        public int BUTTONX_4
        {
            get
            {
                return this.bUTTONX_4;
            }
            set
            {
                this.bUTTONX_4 = value;
                this.RaisePropertyChangedEvent("BUTTONX_4");
            }
        }

        private short bUTTONY_4;
        public short BUTTONY_4
        {
            get
            {
                return this.bUTTONY_4;
            }
            set
            {
                this.bUTTONY_4 = value;
                this.RaisePropertyChangedEvent("BUTTONY_4");
            }
        }

        private short bUTTONLENGTH_4;
        public short BUTTONLENGTH_4
        {
            get
            {
                return this.bUTTONLENGTH_4;
            }
            set
            {
                this.bUTTONLENGTH_4 = value;
                this.RaisePropertyChangedEvent("BUTTONLENGTH_4");
            }
        }

        private short bUTTONWIDTH_4;
        public short BUTTONWIDTH_4
        {
            get
            {
                return this.bUTTONWIDTH_4;
            }
            set
            {
                this.bUTTONWIDTH_4 = value;
                this.RaisePropertyChangedEvent("BUTTONWIDTH_4");
            }
        }

        private int bUTTONVALID_4;
        public int BUTTONVALID_4
        {
            get
            {
                return this.bUTTONVALID_4;
            }
            set
            {
                this.bUTTONVALID_4 = value;
                this.RaisePropertyChangedEvent("BUTTONVALID_4");
            }
        }

        private int bUTTONFORMINDEX_4;
        public int BUTTONFORMINDEX_4
        {
            get
            {
                return this.bUTTONFORMINDEX_4;
            }
            set
            {
                this.bUTTONFORMINDEX_4 = value;
                this.RaisePropertyChangedEvent("BUTTONFORMINDEX_4");
            }
        }

        private DateTime vaildTime;
        public DateTime VaildTime
        {
            get
            {
                return this.vaildTime;
            }
            set
            {
                this.vaildTime = value;
                this.RaisePropertyChangedEvent("VaildTime");
            }
        }

        private int fullScreenWith;
        public int FullScreenWith
        {
            get
            {
                return this.fullScreenWith;
            }
            set
            {
                this.fullScreenWith = value;
                this.RaisePropertyChangedEvent("FullScreenWith");
            }
        }

        private int fullScreenHeight;
        public int FullScreenHeight
        {
            get
            {
                return this.fullScreenHeight;
            }
            set
            {
                this.fullScreenHeight = value;
                this.RaisePropertyChangedEvent("FullScreenHeight");
            }
        }

        private ObservableCollection<Para3083> paramCollection;

        private ImageBrush background;
        public ImageBrush Background
        {
            get
            {
                return this.background;
            }
            set
            {
                this.background = value;
                this.RaisePropertyChangedEvent("Background");
            }
        }

        //Button
        public DelegateCommand AddParamCommand { get; private set; }
        public DelegateCommand DelParamCommand { get; private set; }
        public DelegateCommand SaveParamCommand { get; private set; }
        public DelegateCommand CanclEditParamCommand { get; private set; }
        public DelegateCommand NewVersionCommand { get; private set; }
        public DelegateCommand CopyCurrentVersionCommand { get; private set; }
        public DelegateCommand NewFormIndexCommand { get; private set; }

        public DelegateCommand AddCanvasCommand { get; private set; }


        public DelegateCommand OkChildWindow { get; private set; }

        public DelegateCommand GetCoordinate1Command { get; private set; }


        public DelegateCommand ConfirmCoordinateCommand { get; private set; }

        private string imgPath;
        public string ImgPath
        {
            get
            {
                return this.imgPath;
            }
            set
            {
                this.imgPath = value;
                this.RaisePropertyChangedEvent("ImgPath");
            }
        }


        private PhotoGraphView photoWindow;
        
        [ImportingConstructor]
        public Para3083ViewModel()
        {
            VaildTime = DateTime.Now;
            paramCollection = new ObservableCollection<Para3083>();
            paramDao = new Para3083DAO();
            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
            if (paramDao.GetAllVersion().Count == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("当前参数无任何参数版本");
            }
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;

            if (InfoVerList.CurrentItem != null)
            {
                int infoVer = (int)InfoVerList.CurrentItem;
                paramCollection = paramDao.GetObjectByInfoVer(infoVer);
                FromIndexList = CollectionViewSource.GetDefaultView(paramCollection);
                FromIndexList.CurrentChanged += FromIndexList_CurrentChanged;
            }

            //Button
            this.AddParamCommand = new DelegateCommand(this.AddParam);
            this.NewVersionCommand = new DelegateCommand(this.NewVersion);
            this.NewFormIndexCommand = new DelegateCommand(this.NewFormIndex);
            this.SaveParamCommand = new DelegateCommand(this.SaveParam);
            
            this.DelParamCommand = new DelegateCommand(this.DelParam);
            /*
            this.SaveParamCommand = new DelegateCommand(this.SaveParam);
            this.CanclEditParamCommand = new DelegateCommand(this.CanclEditParam);
           */
            this.CopyCurrentVersionCommand = new DelegateCommand(this.CopyCurrentVersion);

            this.GetCoordinate1Command = new DelegateCommand(this.GetCoordinate1);
            this.AddCanvasCommand = new DelegateCommand(this.AddCanvasToMainView);
            this.ConfirmCoordinateCommand = new DelegateCommand(this.ConfirmCoordinate);
        }

        private void ConfirmCoordinate()
        {
            if (ApplictionDatacontext.TopPoint != null)
            {
                Point? topPoint = ApplictionDatacontext.TopPoint;
                this.BUTTONX_1 = (int)topPoint.Value.X;
                this.BUTTONY_1 = (int)topPoint.Value.Y;
                this.BUTTONLENGTH_1 = 49;
                this.BUTTONWIDTH_1 = 49;
                photoWindow.Close();
            }
            else
            {
                MessageBox.Show("未框选坐标!请进行框选");
            }
            
        }

        private void GetCoordinate1()
        {
            ApplictionDatacontext.TopPoint = null;
            ApplictionDatacontext.BottomPoint = null;

            photoWindow = new PhotoGraphView(this);

            Background = new ImageBrush();
            

            this.ImgPath = this.IMAGENAME;
            Background.ImageSource =
                new BitmapImage(
                    new Uri(this.ImgPath, UriKind.Relative)
                );
            try
            {
                Background.ImageSource =
                new BitmapImage(
                    new Uri(this.ImgPath, UriKind.Relative)
                );
                System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(this.ImgPath);
                this.FullScreenHeight = imgPhoto.Height;
                this.FullScreenWith = imgPhoto.Width;
                photoWindow.ShowDialog();
            }
            catch (Exception e)
            {
                MessageBox.Show("加载图片出错!" + e.Message);
            }
            
        }

        private void AddCanvasToMainView()
        {
            MessageBox.Show("测试");
        }

        private void CopyCurrentVersion()
        {
            ObservableCollection<Para3083> currentVersionParam = new ObservableCollection<Para3083>();
            int infoVer = (int)InfoVerList.CurrentItem;
            currentVersionParam = paramDao.GetObjectByInfoVer(infoVer);

            if (currentVersionParam.Count != 0)
            {
                Para3083 param = null;
                Para3083Pk paramPk = null;
                int newVersion = paramDao.GetMaxID() + 1;
                foreach (Para3083 item in currentVersionParam)
                {
                    param = new Para3083();
                    paramPk = new Para3083Pk();
                    //编辑参数
                    paramPk.infoVer = newVersion;
                    paramPk.formindex = item.para3083Pk.formindex;

                    param.para3083Pk = paramPk;
                    param.validTime = item.validTime;
                    param.recNum = item.recNum;
                    param.imagename = item.imagename;
                    param.imagewidth = item.imagewidth;
                    param.imageheight = item.imageheight;
                    param.buttonx = item.buttonx;
                    param.buttony = item.buttony;
                    param.langlength = item.langlength;
                    param.langwidth = item.langwidth;
                    param.buttonvalid1 = item.buttonvalid1;
                    param.buttonx1 = item.buttonx1;
                    param.buttony1 = item.buttony1;
                    param.buttonlength1 = item.buttonlength1;
                    param.buttonwidth1 = item.buttonwidth1;
                    param.buttonformindex1 = item.buttonformindex1;
                    param.buttonvalid2 = item.buttonvalid2;
                    param.buttonx2 = item.buttonx2;
                    param.buttony2 = item.buttony2;
                    param.buttonlength2 = item.buttonlength2;
                    param.buttonwidth2 = item.buttonwidth2;
                    param.buttonformindex2 = item.buttonformindex2;
                    param.buttonvalid3 = item.buttonvalid3;
                    param.buttonx3 = item.buttonx3;
                    param.buttony3 = item.buttony3;
                    param.buttonlength3 = item.buttonlength3;
                    param.buttonwidth3 = item.buttonwidth3;
                    param.buttonformindex3 = item.buttonformindex3;
                    param.buttonvalid4 = item.buttonvalid4;
                    param.buttonx4 = item.buttonx4;
                    param.buttony4 = item.buttony4;
                    param.buttonlength4 = item.buttonlength4;
                    param.buttonwidth4 = item.buttonwidth4;
                    param.buttonformindex4 = item.buttonformindex4;
                    param.resv11 = item.resv11;
                    param.resv12 = item.resv12;
                    param.resv13 = item.resv13;
                    param.resv14 = item.resv14;
                    param.linecount = item.linecount;

                    ResultValue result = paramDao.CreateObject(param);
                    if (result.Code != 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("复制过程中出现错误!错误信息：" + result.Msg);
                    }
                }
                //刷新
                //ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(newVersion));
                InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所查询的当前版本没有数据,不可进行复制");
            }
        }

        private void DelParam()
        {
            try
            {
                if (FromIndexList.CurrentItem != null)
                {
                    Para3083 param = FromIndexList.CurrentItem as Para3083;

                    if (Microsoft.Windows.Controls.MessageBox.Show("确认删除所选择的参数！", "3083参数", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
                    {
                        //删除
                        ResultValue result = paramDao.DeleteObject(param, param.para3083Pk.infoVer);
                        if (result.Code == 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除成功");
                            //刷新
                            ReflashDataGrid();

                        }
                        else
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除失败!发生错误：" + result.Msg);
                        }
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("未选择要删除的参数");
                }
            }
            catch (Exception e)
            {
            }
        }

        private void AddParam()
        {
            EditGroupBoxVisible = Visibility.Visible;
        }

        private void NewVersion()
        {
            ObservableCollection<int> infoVerCollection = new ObservableCollection<int>();
            infoVerCollection = paramDao.GetAllVersion();
            int newVersion = paramDao.GetMaxID() + 1;
            infoVerCollection.Add(newVersion);
            InfoVerList = CollectionViewSource.GetDefaultView(infoVerCollection);
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
        }

        private void NewFormIndex()
        {
            if (InfoVerList.CurrentItem != null)
            {
                Para3083 param = new Para3083();
                Para3083Pk paramPk = new Para3083Pk();
                param.para3083Pk = paramPk;
                int infoVer = (int)(InfoVerList.CurrentItem);
                paramCollection = paramDao.GetObjectByInfoVer(infoVer);
                if (paramCollection.Count != 0)
                {
                    short tmp = 0;
                    //计算出最大的formIndex
                    foreach (Para3083 item in FromIndexList)
                    {
                        if (tmp < item.para3083Pk.formindex)
                        {
                            tmp = item.para3083Pk.formindex;
                            //param = item;
                        }
                    }
                    tmp += 1;
                    param.para3083Pk.formindex = tmp;
                }
                else
                {
                    Para3083Pk p3083Pk = new Para3083Pk();
                    p3083Pk.infoVer = (int)InfoVerList.CurrentItem;
                    p3083Pk.formindex = 1;
                    param.para3083Pk = p3083Pk;
                }
                
                paramCollection.Add(param);
                FromIndexList = CollectionViewSource.GetDefaultView(paramCollection);
                FromIndexList.CurrentChanged += FromIndexList_CurrentChanged;
            }
        }

        void InfoVerList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (InfoVerList.CurrentItem != null)
            {
                ReflashDataGrid();
            }
        }

        void FromIndexList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (FromIndexList.CurrentItem != null)
            {
                reflash(FromIndexList.CurrentItem as Para3083);
            }
        }

        private void ReflashDataGrid()
        {
            if (InfoVerList.CurrentItem != null)
            {
                int infoVer = (int)InfoVerList.CurrentItem;
                paramCollection = paramDao.GetObjectByInfoVer(infoVer);
                FromIndexList = CollectionViewSource.GetDefaultView(paramCollection);
                FromIndexList.CurrentChanged += FromIndexList_CurrentChanged;
                if (FromIndexList.CurrentItem != null)
                {
                    reflash(FromIndexList.CurrentItem as Para3083);
                }
                else
                {
                    Para3083 param = new Para3083();
                    reflash(param);
                }
            }
        }

        private void reflash(Para3083 param)
        {
            //param = FromIndexList.CurrentItem as Para3083;
            this.IMAGENAME = param.imagename;
            this.IMAGEHEIGHT = param.imageheight;
            this.IMAGEWIDTH = param.imagewidth;
            this.BUTTONX = param.buttonx;
            this.BUTTONY = param.buttony;
            this.LANGLENGTH = param.langlength;
            this.LANGWIDTH = param.langwidth;

            this.BUTTONX_1 = param.buttonx1;
            this.BUTTONY_1 = param.buttony1;
            this.BUTTONLENGTH_1 = param.buttonlength1;
            this.BUTTONWIDTH_1 = param.buttonwidth1;
            this.BUTTONVALID_1 = param.buttonvalid1;
            this.BUTTONFORMINDEX_1 = param.buttonformindex1;


            this.BUTTONX_2 = param.buttonx2;
            this.BUTTONY_2 = param.buttony2;
            this.BUTTONLENGTH_2 = param.buttonlength2;
            this.BUTTONWIDTH_2 = param.buttonwidth2;
            this.BUTTONVALID_2 = param.buttonvalid2;
            this.BUTTONFORMINDEX_2 = param.buttonformindex2;

            this.BUTTONX_3 = param.buttonx3;
            this.BUTTONY_3 = param.buttony3;
            this.BUTTONLENGTH_3 = param.buttonlength3;
            this.BUTTONWIDTH_3 = param.buttonwidth3;
            this.BUTTONVALID_3 = param.buttonvalid3;
            this.BUTTONFORMINDEX_3 = param.buttonformindex3;

            this.BUTTONX_4 = param.buttonx4;
            this.BUTTONY_4 = param.buttony4;
            this.BUTTONLENGTH_4 = param.buttonlength4;
            this.BUTTONWIDTH_4 = param.buttonwidth4;
            this.BUTTONVALID_4 = param.buttonvalid4;
            this.BUTTONFORMINDEX_4 = param.buttonformindex4;
            this.VaildTime = DateTime.Now;

            //this.VaildTime = DateTime.ParseExact(param.validTime, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
        }

        public void SaveParam()
        {
            Para3083 param = new Para3083();
            DateTime dt = VaildTime;
            Para3083Pk paramPk = new Para3083Pk();
            
            paramPk.infoVer = (int)InfoVerList.CurrentItem;
            paramPk.formindex = (short)(FromIndexList.CurrentItem as Para3083).para3083Pk.formindex;
            param.para3083Pk = paramPk;
            param.validTime = dt.ToString("yyyyMMddHHmmss");
            if (this.IMAGENAME != null)
            {
                param.imagename = Path.GetFileName(this.IMAGENAME);
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("信息录入不全，无法完成保存!");
                return;
            }
            param.imageheight = this.IMAGEHEIGHT;
            param.imagewidth = this.IMAGEWIDTH;
            param.buttonx = this.BUTTONX;
            param.buttony = this.BUTTONY;
            param.langlength = this.LANGLENGTH;
            param.langwidth = this.LANGWIDTH;

            param.buttonx1 = this.BUTTONX_1;
            param.buttony1 = this.BUTTONY_1;
            param.buttonlength1 = this.BUTTONLENGTH_1;
            param.buttonwidth1 = this.BUTTONWIDTH_1;
            param.buttonvalid1 = this.BUTTONVALID_1;
            param.buttonformindex1 = this.BUTTONFORMINDEX_1;


            param.buttonx2 = this.BUTTONX_2;
            param.buttony2 = this.BUTTONY_2;
            param.buttonlength2 = this.BUTTONLENGTH_2;
            param.buttonwidth2 = this.BUTTONWIDTH_2;
            param.buttonvalid2 = this.BUTTONVALID_2;
            param.buttonformindex2 = this.BUTTONFORMINDEX_2;

            param.buttonx3 = this.BUTTONX_3;
            param.buttony3 = this.BUTTONY_3;
            param.buttonlength3 = this.BUTTONLENGTH_3;
            param.buttonwidth3 = this.BUTTONWIDTH_3;
            param.buttonvalid3 = this.BUTTONVALID_3;
            param.buttonformindex3 = this.BUTTONFORMINDEX_3;

            param.buttonx4 = this.BUTTONX_4;
            param.buttony4 = this.BUTTONY_4;
            param.buttonlength4 = this.BUTTONLENGTH_4;
            param.buttonwidth4 = this.BUTTONWIDTH_4;
            param.buttonvalid4 = this.BUTTONVALID_4;
            param.buttonformindex4 = this.BUTTONFORMINDEX_4;

            param.recNum = 0;
            param.resv11 = 0;
            param.resv12 = 0;
            param.resv13 = 0;
            param.resv14 = 0;

            ResultValue result = paramDao.CreateObject(param);
            if (result.Code == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("保存成功");
                ReflashDataGrid();

                CanclEditParam();
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result.Msg);
            }
        }

        private void CanclEditParam()
        {
            this.EditGroupBoxVisible = Visibility.Hidden;
            this.IMAGENAME = null;
            this.IMAGEHEIGHT = 0;
            this.IMAGEWIDTH = 0;
            this.BUTTONX = 0;
            this.BUTTONY = 0;
            this.LANGLENGTH = 0;
            this.LANGWIDTH = 0;

            this.BUTTONX_1 = 0;
            this.BUTTONY_1 = 0;
            this.BUTTONLENGTH_1 = 0;
            this.BUTTONWIDTH_1 = 0;
            this.BUTTONVALID_1 = 0;
            this.BUTTONFORMINDEX_1 = 0;


            this.BUTTONX_2 = 0;
            this.BUTTONY_2 = 0;
            this.BUTTONLENGTH_2 = 0;
            this.BUTTONWIDTH_2 = 0;
            this.BUTTONVALID_2 = 0;
            this.BUTTONFORMINDEX_2 = 0;

            this.BUTTONX_3 = 0;
            this.BUTTONY_3 = 0;
            this.BUTTONLENGTH_3 = 0;
            this.BUTTONWIDTH_3 = 0;
            this.BUTTONVALID_3 = 0;
            this.BUTTONFORMINDEX_3 = 0;

            this.BUTTONX_4 = 0;
            this.BUTTONY_4 = 0;
            this.BUTTONLENGTH_4 = 0;
            this.BUTTONWIDTH_4 = 0;
            this.BUTTONVALID_4 = 0;
            this.BUTTONFORMINDEX_4 = 0;
        }

        public void Image_MouseDown_CanExecuteChanged(object sender, System.EventArgs ep)
        {
            MessageBox.Show("Mouse Down");
        }
    }
}
